package uit.minito.views;

import java.util.ArrayList;
import java.util.Vector;

import uit.minito.timduongxebuyt.R;
import uit.minito.timduongxebuyt.StaticVariables;
import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ResultNavigator extends RelativeLayout implements OnClickListener {
	Button leftResultButton;
	Button rightResultButton;
	TextView resultOrderTextView;
	TextView resultRouterTextView;
	TextView distanceTextView;  
	TextView timeSpentTextView;    
	LinearLayout mainLayout;  
	OnTouchResultNavigatorListener onTouchResultNavigatorListener;
	boolean isOffScreen;  

	private void initialize(Context context) {    
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.navigate_result_full, this);

		leftResultButton = (Button) findViewById(R.id.leftResultButton);

		leftResultButton.setOnClickListener(this);
		rightResultButton = (Button) findViewById(R.id.rightResultButton);
		rightResultButton.setOnClickListener(this);
		resultOrderTextView=(TextView)findViewById(R.id.resultOrderTextView);
		resultRouterTextView=(TextView)findViewById(R.id.resultRouterTextView);
		distanceTextView=(TextView)findViewById(R.id.distanceTextView);
		timeSpentTextView=(TextView)findViewById(R.id.timeSpentTextView);
		mainLayout=(LinearLayout)findViewById(R.id.navigatorMainLayout);
		isOffScreen=true;
		
		RelativeLayout.LayoutParams mainParam=(RelativeLayout.LayoutParams)mainLayout.getLayoutParams();

		mainParam.topMargin=MyUtilities.convertToDP(StaticVariables.NAVIGATOR_CONTENT_HEIGHT, getContext());
		mainParam.bottomMargin=mainParam.topMargin+MyUtilities.convertToDP(StaticVariables.NAVIGATOR_TOTAL_HEIGHT, getContext());
		mainLayout.setLayoutParams(mainParam);
		
		resultOrderTextView.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(isOffScreen){
					display();
				}else{
					hide();
				}
			}
		});
		
		reset();
		
	}

	public void setOnTouchResultNavigatorListener(
			OnTouchResultNavigatorListener _delegate) {
		onTouchResultNavigatorListener = _delegate;
	}

	public ResultNavigator(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public ResultNavigator(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public ResultNavigator(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);
	}
	public void reset(){
		resultOrderTextView.setText(getResources().getString(R.string.result_info));

		resultRouterTextView.setText("");
		distanceTextView.setText("");
		timeSpentTextView.setText("");
		leftResultButton.setEnabled(false);
		rightResultButton.setEnabled(false);
		
	}
	  
	public void setInfo(int numberOfResults, int currentResultIndex,int numberOfRouters,ArrayList<String> routerNameList,double distanceInKm,String timeSpent){
		String resultOrderString=getResources().getString(R.string.result_ketquathu)+" "+ MyUtilities.makeBoldAndBiggerText(String.valueOf(currentResultIndex+1))+" / "+MyUtilities.makeBoldAndBiggerText(String.valueOf(numberOfResults));
		resultOrderTextView.setText(Html.fromHtml(resultOrderString)); 
		ArrayList<Integer> listRouterNameInInt = MyUtilities.convertRouterNameArrayToIntArray(routerNameList);
		String comboRouterString=""; 
		String tuyenString=getResources().getString(R.string.result_router_tuyen);
		String vaString=getResources().getString(R.string.result_router_va);
		String diString=getResources().getString(R.string.result_router_di);
		Vector<String> colorArray=new Vector<String>();
		colorArray.add("red");
	    colorArray.add("green");
	    colorArray.add("blue"); 
		comboRouterString+=MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText((String.valueOf(listRouterNameInInt.get(0).intValue()))),colorArray.get(0));// tuyen 1
		for(int i=1;i<listRouterNameInInt.size();i++){
			int iNameInInt=listRouterNameInInt.get(i).intValue();      
			if(i==listRouterNameInInt.size()-1){
				comboRouterString+=" "+vaString+" "+MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText(String.valueOf(iNameInInt)),colorArray.get(i));
			}else{
				comboRouterString+=", "+MyUtilities.makeColorToText(MyUtilities.makeBoldAndBiggerText(String.valueOf(iNameInInt)),colorArray.get(i)); 
			}
		}
		String resultRouterString=diString+" "+MyUtilities.makeBoldAndBiggerText(String.valueOf(numberOfRouters))+" "+tuyenString+" : "+comboRouterString;
		resultRouterTextView.setText(Html.fromHtml(resultRouterString)); 
		String distanceStringFromRes=getResources().getString(R.string.result_distance);
		String kmFromRes=getResources().getString(R.string.result_km);
		String distanceString=String.valueOf(MyUtilities.roundDouble(distanceInKm,1))+" "+kmFromRes;
		distanceTextView.setText(distanceString);
		String timeSpentFromRes=getResources().getString(R.string.result_timeSpent);
		String phutString=getResources().getString(R.string.result_phut);
		String timeSpentString=timeSpent +" "+phutString;
		timeSpentTextView.setText(timeSpentString);
		
		
		leftResultButton.setEnabled(true);
		rightResultButton.setEnabled(true);
	}  
	  
	private void doSlideAnimation(float _fromY,final float _toY){
		TranslateAnimation translateAnimation=new TranslateAnimation(0, 0, _fromY, MyUtilities.convertToDP(_toY, getContext()));
		translateAnimation.setDuration(StaticVariables.SLIDE_DURATION);
		translateAnimation.setAnimationListener(new Animation.AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}  
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				
				RelativeLayout.LayoutParams mainParam=(RelativeLayout.LayoutParams)mainLayout.getLayoutParams();
				if(isOffScreen){
					mainParam.topMargin=0;
				}else{
					mainParam.topMargin=MyUtilities.convertToDP(_toY, getContext());
				}
				mainParam.bottomMargin=mainParam.topMargin+MyUtilities.convertToDP(_toY, getContext());
				mainLayout.setLayoutParams(mainParam);
				isOffScreen=!isOffScreen;
				mainLayout.clearAnimation();
			}
		});
		mainLayout.startAnimation(translateAnimation);
	}


	public void display() {
		if(isOffScreen){
			doSlideAnimation(0, -StaticVariables.NAVIGATOR_CONTENT_HEIGHT);
		}
		   
	}

	public void hide() {
		if (isOffScreen==false) {
			doSlideAnimation(0, StaticVariables.NAVIGATOR_CONTENT_HEIGHT);
			
		}
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.leftResultButton:
			this.onTouchResultNavigatorListener.onTouchPrev();
			break;

		case R.id.rightResultButton:
			this.onTouchResultNavigatorListener.onTouchNext();
			break;
		}

	}

}
